﻿/*
 * Forum Service
 *
 * NOTE:
 *
 *      More features (such as access frequency control, refresh interval limition) will be added in M3 (beta)
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Snowdreamist.Collection;
using Snowdreamist.Threading;
using SNSpirit.Domain.Subscription;

namespace SNSpirit.Domain.Forum
{
    public class ForumService : DomainService
    {
        public ForumService(IForumServiceConnector connector)
        {
            if (connector == null)
                throw new ArgumentNullException("connector");
            this._connector = connector;
            // root region
            this.RootRegion = connector.CreateRootRegion(this);
            this.RootRegion.LoadAsync();
        }

        public virtual Post NewPost()
        {
            var post = this._connector.CreateEmptyPost(this);
            // do something
            return post;
        }

        public virtual Thread NewThread()
        {
            var thread = this._connector.CreateEmptyThread(this);
            // do something
            return thread;
        }

        /// <summary>
        /// Send new thread
        /// 发新帖
        /// NOTE:
        ///     region maybe null
        /// </summary>
        public virtual void SendThread(Region region, Thread newThread)
        {
            if (newThread == null)
                throw new ArgumentNullException("newThread");
            if (!object.ReferenceEquals(this, newThread.Service))
                throw new InvalidOperationException("Service mismatch");
            // todo
            this._connector.SendNewThread(region, newThread);
        }

        /// <summary>
        /// Reply a thread
        /// 回复一个帖子
        /// </summary>
        public virtual void ReplyThread(Thread thread, Post newPost)
        {
            if (thread == null)
                throw new ArgumentNullException("thread");
            if (newPost == null)
                throw new ArgumentNullException("newPost");
            if (!object.ReferenceEquals(this, thread.Service))
                throw new InvalidOperationException("Service mismatch");
            if (!object.ReferenceEquals(this, newPost.Service))
                throw new InvalidOperationException("Service mismatch");
            // todo
            this._connector.SendNewPost(thread, newPost);
        }

        public Region RootRegion { get; private set; }

        public SubscriptionService<Thread> Subscription
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        #region internal

        internal void UpdateCore(Region region)
        {
            // todo
            this._connector.Update(region);
        }

        internal void UpdateCore(Thread thread)
        {
            // todo
            this._connector.Update(thread);
        }

        internal void UpdateCore(Post post)
        {
            // todos
            this._connector.Update(post);
        }

        #endregion internal

        #region private

        private IForumServiceConnector _connector;

        #endregion private
    }
}